// 引入vue路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { getLocal } from '@/utils/local.js'
import { auInfo } from '@/api/my.js'

Vue.use(VueRouter)
// 每个对象对相应的页面跳转路由
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  { path: '/login', component: () => import('@/views/login.vue') },
  {
    path: '/home',
    component: () => import('@/views/home/home.vue'),
    redirect: '/home/my',
    children: [
      {
        path: '/home/company',
        component: () => import('@/views/home/company/company.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: '/home/question',
        component: () => import('@/views/home/question/question.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: '/home/questionInfo/:id?',
        component: () => import('@/views/home/question/questionInfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: '/home/find',
        component: () => import('@/views/home/find/find.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: '/home/shareList',
        component: () => import('@/views/home/find/shareList.vue'),
        meta: {
          // needTab: true
        }
      },
      {
        path: '/home/shareinfo/:id?',
        component: () => import('@/views/home/find/shareinfo.vue'),
        meta: {
          // needTab: true
        }
      },
      {
        path: '/home/my',
        component: () => import('@/views/home/my/my.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: '/home/myinfo',
        component: () => import('@/views/home/my/myinfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: '/home/eidtInfo',
        component: () => import('@/views/home/my/eidtInfo.vue'),
        meta: {
          needLogin: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 前置守卫
router.beforeEach(async (to, from, next) => {
  // window.cancelAxios('', true)
  const _scrolltop =
    document.documentElement.scrollTop ||
    document.body.scrollTop ||
    window.pageYOffset
  from.meta.scrollTop = _scrolltop
  // window.console.log('scrollTop', _scrolltop)
  if (!to.meta.needLogin) {
    next()
    if (getLocal('token') && !store.state.isLogin) {
      const res = await auInfo(true)
      store.commit('setUserInfo', res.data.data)
      store.commit('setIsLogin', true)
    }
  } else {
    if (store.state.isLogin) {
      next()
    } else {
      if (getLocal('token')) {
        // 获取用户信息保存后next
        auInfo()
          .then(res => {
            store.commit('setUserInfo', res.data.data)
            store.commit('setIsLogin', true)
            next()
          })
          .catch(() => {})
      } else {
        next('/login?redirect=' + to.fullPath)
      }
    }
  }
})
router.afterEach((to, from) => {
  window.scrollTo(0, 0)
})

export default router
